package com.saleoffline.server.domain.member;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.saleoffline.server.BaseController;
import com.saleoffline.server.PageNavigator;
import com.saleoffline.server.SaleException;

@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {
	@RequestMapping("/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		Member member = memberDAO.select(id);
		ModelAndView mav = new ModelAndView("member/form");
		mav.addObject("member", member);
		return mav;
	}

	@RequestMapping("/insert")
	public ModelAndView insert(Member member, HttpServletRequest request) throws Exception {
		savePhoto(member, request);
		int id = memberDAO.insert(member);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/member/view")));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/update")
	public ModelAndView update(Member member, HttpServletRequest request) throws Exception {
		savePhoto(member, request);
		memberDAO.update(member);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/member/view")));
		mav.addObject("id", member.getId());
		return mav;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(int id) {
		memberDAO.delete(id);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/member/list")));
		return mav;
	}
	
	@RequestMapping("/view")
	public ModelAndView view(int id) {
		Member member = memberDAO.select(id);
		if (member == null) {
			throw new SaleException("message");
		}
		ModelAndView mav = new ModelAndView("member/view");
		mav.addObject("member", member);
		return mav;
	}	
	
	@RequestMapping("/list")
	public ModelAndView list(MemberSearch search, PageNavigator pageNavigator) {
		search.setOffset(pageNavigator.getOffset());
		search.setLimit(pageNavigator.getListSize());
		List<Member> memberList = memberDAO.search(search);
		
		ModelAndView mav = new ModelAndView("member/list");
		mav.addObject("search", search);
		mav.addObject("pageNavigator", pageNavigator);
		mav.addObject("memberList", memberList);
		return mav;
	}
	
	private void savePhoto(Member member, HttpServletRequest request) throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		List<MultipartFile> files = multipartRequest.getFiles("photos");
		
		if (files != null) {
			for (MultipartFile each : files) {
				String fileName = member.getPhoneNo() + "_" + each.getOriginalFilename();
				FileUtils.writeByteArrayToFile(new File("d:/dev/storage/photo/" + fileName), each.getBytes());
				member.setProfileImageUrl("http://jarang.iptime.org/profile/" + fileName);
			}
		}
	}
}
